package controllers;

import java.util.ArrayList;
import java.util.List;

import models.Exercise;
import models.Notification;
import models.Question;
import models.RegisteredUser;
import models.ReviewRequest;
import models.Tutorial;
import play.mvc.Controller;

public class ReviewRequests extends Controller {

	/***
	 * @author dana This method finds all the reviewers from the registered users
	 *         table
	 * @return list of reviewers
	 */
	public static List<RegisteredUser> findReviewers() {

		int size = (RegisteredUser.findUsers().size());

		List<RegisteredUser> allusers = RegisteredUser.findAll();

		List<RegisteredUser> reviewersList = new ArrayList<RegisteredUser>();

		for (int i = 0; i < size; i++) {

			if (allusers.get(i).getReviewer() == true) {

				reviewersList.add(allusers.get(i));

			}
		}
		return reviewersList;
	}

	/***
	 * @author dana
	 * @param exerciseId
	 *            This method sends review requests to reviewers to review a
	 *            certain exercise
	 */
	public static void sendRequests(long exerciseId) {

		List<RegisteredUser> reviewers = findReviewers();

		long creatorID = Long.parseLong(session.get("RegisteredUserId"));

		for (int i = 0; i < reviewers.size(); i++) {

			ReviewRequest x = new ReviewRequest(creatorID,
					(long) reviewers.get(i).id, reviewers.get(i).name,
					exerciseId).save();

			reviewers.get(i).listOfReviewRequest.add(x);

			reviewers.get(i).save();

		}
		Application.viewProfile(creatorID);
	}

	/***
	 * @author dana This method opens the review requests sent to the reviewers
	 */
	public static void openRequest() {

		long reviewerID = Long.parseLong(session.get("RegisteredUserId"));

		RegisteredUser user = RegisteredUser.findById(reviewerID);

		List<ReviewRequest> RRequests = user.listOfReviewRequest();

		render(RRequests);

	}

	/***
	 * @author dana
	 * @param open
	 *            This method takes the value of the openRequest and renders the
	 *            exercise page with the content of that exercise.
	 */

	public static void viewExercise(long open) {

		try {
			models.Exercise exercise = models.Exercise.findById(open);

			if (exercise.isRandom() == true) {

				List<Question> questions;

				Exercise ex = Exercises.listOfRandomQuestions(open);

				questions = ex.getQuestions();

				render(questions, open);
			} else {

				List<Question> questions;

				questions = exercise.getQuestions();

				render(questions, open);

			}
		} catch (NullPointerException e) {

			renderText("Oopss...a null pointer exception was found :ssss");
		}
	}

	/***
	 * @author dana
	 * @author Omnia - Notification
	 * @param accept
	 *            This method accepts a certain exercise
	 */
	public static void publishExercise(long accept) {

		Exercise exercise = Exercise.findById(accept);

		long reviewerID = Long.parseLong(session.get("RegisteredUserId"));

		if (exercise.isPublished() == false && exercise.isRejected() == false) {

			exercise.setPublished(true);

			exercise.save();

			new Notification(reviewerID, exercise.getCreatorID(), exercise.id,
					26).save();

			Application.viewProfile(reviewerID);

			renderText("Thank you for accepting the Exercise :) !!!");

		} else {

			if (exercise.isPublished() == true) {

				renderText("Oops...You can't publish a published Exercise !!!");
			} else {

				renderText("Oops... You can't publish a rejected Exercise !!!");
			}
		}
	}

	/***
	 * @author dana
	 * @author Omnia - Notification
	 * @param reject
	 *            This method rejects a certain exercise
	 */
	public static void RejectExercise(long reject) {

		Exercise exercise = Exercise.findById(reject);

		long reviewerID = Long.parseLong(session.get("RegisteredUserId"));

		if (exercise.isRejected() == false && exercise.isPublished() == false) {

			exercise.setRejected(true);

			exercise.save();

			new Notification(reviewerID, exercise.getCreatorID(), exercise.id,
					27).save();

			Application.viewProfile(reviewerID);

			renderText("This exercise is now rejected !!!");

		} else {
			if (exercise.isRejected() == true) {

				renderText("Oops...You can't reject a rejected Exercise !!!");

			}
		}

		renderText("Oops...You can't reject a published exercise at the moment :(  !!!");
	}

	/***
	 * @author dana This method returns a list of exercises created by a creator
	 */
	public static void myCreatedExercises() {

		long user = Long.parseLong(session.get("RegisteredUserId"));

		List<Exercise> myExercises;

		myExercises = Exercise.find("creatorId=?", user).fetch();

		render(myExercises);
	}
}
